class Solution:
    def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
        res = []
        direction = 0
        # 0 向右 1 向下
        # 2 向左 3 向上

        # y 第一维度
        top, bottom = len(matrix) - 1, 0 
        
        # x 第二维度
        left, right = 0, len(matrix[0]) - 1
        

        x,y = 0,0
        while top != bottom or left != right:
            direction = direction % 4
            while (left <= x <= right and bottom <= y <= top):
                if direction == 0:
                    x += 1
                elif direction == 1:
                    y += 1
                elif direction == 2:
                    x -= 1
                elif direction == 3:
                    y -= 1
                res.append(matrix[y][x])
                if direction == 0 and x == right:
                    direction += 1
                    right -= 1





